/*
 * The MIT License
 *
 * Copyright 2011 Roman Yakovenko <erelus@gmail.com>.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

import com.stego.alg.WaveletAlg;
import com.stego.image.Image;
import com.stego.image.Pixel;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class Main {

    private static final int ERROR_CODE = 1;

    public static void main(String[] args) throws IOException {
        if (args.length < 2) {
            printHelp();
            System.exit(ERROR_CODE);
        }

        if (args[0].equals("-h")) {
            hide(args[1]);
        } else {
            if (args[0].equals("-d")) {
                discover(args[1]);
            } else {
                printHelp();
                System.exit(ERROR_CODE);
            }
        }

        System.exit(0);
    }

    private static void hide(String path) {
        WaveletAlg waveletAlg = new WaveletAlg(new File(path));
        Scanner scanner = new Scanner(System.in);
        System.out.print("Input text to hide:");
        String data = scanner.next();
        try {
            Pixel[][] result = waveletAlg.hideData(data.getBytes());
            System.out.print("Input path to file for saving:");
            String resultPath = scanner.next();
            Image image = new Image(new File(resultPath));
            image.setPixels(result);
            image.save();
            System.out.println(String.format("File %s successfully saved.", resultPath));
        } catch (IOException e) {
            System.out.println(e.getMessage());
            System.exit(ERROR_CODE);
        }
    }

    private static void discover(String path) {
        WaveletAlg waveletAlg = new WaveletAlg(new File(path));
        try {
            byte[] data = waveletAlg.getData();
            String result = new String(data);
            System.out.println(String.format("Extracted text: %s", result));
        } catch (IOException e) {
            System.out.println(e.getMessage());
            System.exit(ERROR_CODE);
        }
    }

    private static void printHelp() {
        System.out.println("Usage: wavelet (-h|-d) file");
        System.out.println("-h - hide text.");
        System.out.println("-d - discover text.");
    }
}
